package com.soviet.boot.business.system.service.impl;

import com.baomidou.mybatisplus.annotation.OrderBy;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.soviet.boot.business.system.entity.SysUser;
import com.soviet.boot.business.system.mapper.SysUserMapper;
import com.soviet.boot.business.system.service.ISysUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.soviet.boot.business.system.vo.SysUserVo;
import org.apache.ibatis.builder.MapperBuilderAssistant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 系统用户 服务实现类
 * </p>
 *
 * @author Dave
 * @since 2021/07/20
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {

    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public Object addUser(SysUser sysUser) {
        String encodePassword = passwordEncoder.encode(sysUser.getPassword());
        sysUser.setPassword(encodePassword);
        return this.save(sysUser);
    }

    @Override
    public Object pageList(PageDTO<SysUserVo> page, SysUserVo query) {

        LambdaQueryWrapper<SysUserVo> wrapper = Wrappers.lambdaQuery(query);
        wrapper.like(!StringUtils.isBlank(query.getUsername()),SysUserVo::getUsername,query.getUsername());
        wrapper.like(!StringUtils.isBlank(query.getRoleName()),SysUserVo::getRoleName,query.getRoleName());
        return sysUserMapper.pageList(page,wrapper);

    }

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        SysUser user = this.getOne(new LambdaQueryWrapper<SysUser>()
                .eq(SysUser::getUsername, s));
        return user;
    }

    @Override
    public Object accountExist(String account) {
        LambdaQueryWrapper<SysUser> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(SysUser::getUsername,account);

        return this.getOne(wrapper)==null;
    }
}
